package leetCode.hw.dp;

public class HJ32Solution implements HJ32 {

    @Override
    public int getMaxSymmetrySub(String str) {
        int result = 0;
        int i = 0;
        int end = str.length();
        while(i<=end){
            int tem = getMaxSymLength(str.substring(i,end));
            if(result < tem){
                result = tem;
            }
            i++;
            end--;
        }
        return result;
    }

    /**
     * 判断一个字符串是否是对称的
     * @param str
     * @return
     */
    public static boolean isSym(String str) {
        boolean result = true;
        int length = str.length() - 1;
        for (int i = 0; i < str.length() / 2; i++) {
            if (str.charAt(i) != str.charAt(length)) {
                result = false;
                break;
            }
            length--;
        }
        return result;
    }

    /**
     * 从前往后,从后往前找出最长的符合规则的字符串长度
     * @param str
     * @return
     */
    public static int getMaxSymLength(String str) {
        int result = 0;

        for(int i=1;i<str.length();i++){
            String sub = str.substring(0,i);
            if(isSym(sub)&&sub.length()>result){
                result = sub.length();
            }
        }

        for(int i=str.length()-1;i>=0;i--){
            String sub = str.substring(i);
            if(isSym(sub)&&sub.length()>result){
                result = sub.length();
            }
        }
        return result;
    }


}
